<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/12/25
 * Time: 11:52
 */

namespace app\admin\controller\crm;

use app\common\controller\Backend;
use think\Db;
use think\Session;
use app\admin\model\AuthGroup;
class Group extends Backend
{
    /**
     * 销售专员分组查询
     */
    public function admin_group(){
        $this->dataLimit = '';
        $this->dataLimitField = '';
        $pid = $this->request->get('pid');
        /*$userData = Session::get("admin");*/
        if($pid){
            //查询客服
            $groupArray = array();
            $this->di_group($pid,$groupArray);
            $linkAdmin = Db::name('Admin');
            $adminData = $linkAdmin->field('a.id AS value,a.nickname AS name')->alias('a')
                ->join('fa_auth_group_access g','a.id = g.uid','LEFT')
                ->where(array('g.group_id'=>array('IN',$groupArray)))->select();
            $this->success('','',$adminData);
        }else{
            //默认查询所有下级销售专员
            $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
            $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();//获取用户权限
            $groupidArray = array();
            foreach ($groupList as $k=>$v){
                if($v['id']!=10&&$v['id']!=11&&$v['id']!=12){
                    $groupidArray[] = $v['id'];
                }
            }
            $linkAdmin = Db::name('admin');
            $adminData = $linkAdmin->field('a.id AS value,a.nickname AS name')->alias('a')
                ->join('fa_auth_group_access c','a.id = c.uid','LEFT')
                ->where(array('c.group_id'=>array('IN',$groupidArray)))->select();
            $this->success('','',$adminData);
        }
    }
}